package thread;

public class ThreadDemo20 {
    //有三个线程，控制三个线程分别按照 ABC 的顺序打印出来
    public static void main(String[] args) throws InterruptedException{
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(() -> {
            System.out.println('A');
            synchronized (locker1) {
                //告知线程2B可以打印了
                locker1.notify();
            }
        });

        Thread t2 = new Thread(() -> {
            //等待线程1打印完A再打印B
            synchronized (locker1) {
                try {
                    locker1.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println('B');
            synchronized (locker2) {
                //告知线程3C可以打印了
                locker2.notify();
            }
        });

        Thread t3 = new Thread(() -> {
            synchronized (locker2) {
                try {
                    locker2.wait();//等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println('C');
        });

        t2.start();
        t3.start();
        Thread.sleep(1000);
        t1.start();
    }
}
